package com.traffic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.traffic.constant.Const;
import com.traffic.service.BaidumapService;
import com.traffic.util.BaidumapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class BaidumapServiceImpl implements BaidumapService{
    @Autowired
    BaidumapUtil baidumapUtil;

    @Autowired
    RestTemplate restTemplate;

    @Override
    public String getRoadname(Map<String, Object> params) {

        //http://api.map.baidu.com/traffic/v1/around?ak=你的AK&center=39.912078,116.464303&radius=200&coord_type_input=gcj02&coord_type_output=gcj02//GET请求

        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"traffic/v1/around"+"?ak={ak}&center={center}&radius={radius}", String.class,params);

        JSONObject json = JSON.parseObject(jsr);
        JSONArray road_traffic= JSON.parseArray(json.get("road_traffic").toString());
        JSONObject road = road_traffic.getJSONObject(0);

        String roadname = road.get("road_name").toString();

        return roadname;
    }

    @Override
    public String getCityname(Map<String, Object> params) {
        //http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194  //GET请求
        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"reverse_geocoding/v3/"+"?ak={ak}&location={location}&output=json", String.class,params);

        JSONObject json = JSON.parseObject(jsr);
        JSONObject ac= json.getJSONObject("result").getJSONObject("addressComponent");
        String city = ac.getString("city");

        return city;
    }

    @Override
    public String getRoadtraffic(Map<String, Object> params) {
        //http://api.map.baidu.com/traffic/v1/road?road_name=东二环&city=北京市&ak=你的AK //GET请求
        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"traffic/v1/road"+"?ak={ak}&road_name={road_name}&city={city}", String.class,params);

      //  JSONObject json = JSON.parseObject(jsr);


        return jsr;
    }

    @Override
    public String getAddress(Map<String, Object> params) {

        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"reverse_geocoding/v3/"+"?ak={ak}&location={location}&extensions_poi=1&output=json", String.class,params);

        return jsr;
    }

    @Override
    public String getLocation(Map<String, Object> params) {
        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"geocoding/v3/"+"?ak={ak}&address={address}&output=json", String.class,params);

        return jsr;
    }

    @Override
    public String CityGetLocation(Map<String, Object> params) {
        String jsr = restTemplate.getForObject(Const.BAIDUMAP_API+"geocoding/v3/"+"?ak={ak}&address={address}&output=json", String.class,params);

        JSONObject json = JSON.parseObject(jsr).getJSONObject("result");
        JSONObject detail = json.getJSONObject("location");
        String lng = detail.getString("lng");
        String lat = detail.getString("lat");

        return lng+","+lat;
    }
}
